﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication5.Models;

namespace WebApplication5.Controllers
{
    
    [ApiController]
    [Route("api/order")]
    public class OrderServiceController : ControllerBase
    {
        private OrderService orderService;

        public OrderServiceController(OrderService orderService)
        {
            this.orderService = orderService;
        }

        // POST: api/Order
        [HttpPost]
        public ActionResult<OrderDb> addOrder(Order order)
        {
            try
            {
                order.orderId = Guid.NewGuid().ToString();
                orderService.addOrder(order);
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }

            return order;
        }

        // DELETE: api/Order/1
        [HttpDelete("{id}")]
        public ActionResult<OrderDb> deleteOrder(string id)
        {
            try
            {
                orderService.deleteOrder(id);
            }
            catch (MyException e)
            {
                return BadRequest(e.Message);
            }
            return NoContent();
        }

        // PUT: api/Order/1
        [HttpPut("{id}","{name}")]
        public ActionResult<OrderDb> modifyCoustomer(string id,string name)
        {
            try
            {
                orderService.modifyCustomer(id,name);
            }
            catch (MyException e)
            {
                return BadRequest(e.Message);
            }
            return NoContent();
        }

        // PUT: api/Order/1
        [HttpPut("{id}", "{date}")]
        public ActionResult<OrderDb> modifyDate(string id, string date)
        {
            try
            {
                orderService.modifyDate(id, date);
            }
            catch (MyException e)
            {
                return BadRequest(e.Message);
            }
            return NoContent();
        }

        // PUT: api/Order/1
        [HttpPut("{id}", "{index}","{num}","{price}")]
        public ActionResult<OrderDb> modifyOrderDetail(string id, int index, int num, double price)
        {
            try
            {
                orderService.modifyOrderDetail(id, index,num,price);
            }
            catch (MyException e)
            {
                return BadRequest(e.Message);
            }
            return NoContent();
        }

        // GET: api/Order/1
        [HttpGet("{id}")]
        public ActionResult<OrderDb> findID(string id)
        {
            var order = orderService.findID(id);
            if (order == null)
            {
                return NotFound();
            }
            return order;
        }

        // GET: api/Order
        [HttpGet("{name}")]
        public ActionResult<List<OrderDb>> findCustomer(string name)
        {
            return orderService.findCustomer (name);
        }

        // GET: api/Order
        [HttpGet("{money}")]
        public ActionResult<List<OrderDb>> findMoney(double money)
        {
            return orderService.findMoney(money);
        }

        // GET: api/Order
        [HttpGet("{date}")]
        public ActionResult<List<OrderDb>> findOrderDate(string date)
        {
            return orderService.findOrderDate(date);
        }

        // GET: api/Order
        [HttpGet("{name}")]
        public ActionResult<List<OrderDb>> findGoods(string name)
        {
            return orderService.findGoods(name);
        }

   

        
    }
